package com.nari.ysphd.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jeremyfeinstein.gson.Location;
import com.jeremyfeinstein.gson.UserItem;
import com.jeremyfeinstein.utils.YCHSInstance;
import com.nari.ysphd.R;
import com.nari.ysphd.adapter.NewBaseAdapter;

import java.util.ArrayList;
import java.util.List;

/*
 * 消息弹框
 */
public class LocationDialog extends PopupWindow {

    private View mMenuView;
    public Activity window;
    private NewBaseAdapter mAdapter;
    private ListView dialog_list;
    private EditText input_name;

    public LocationDialog(final Activity context, String queryText, NewBaseAdapter adapter) {
        super(context);
        window = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.view_dialoglayout, null);
        dialog_list = (ListView) mMenuView.findViewById(R.id.dialog_list);
//        input_name = (EditText) mMenuView.findViewById(R.id.input_name);
        mAdapter = new LocationAdapter();
//        mAdapter = adapter;
//        mAdapter = new LocationAdapter();
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

      /*  input_name.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                mAdapter.refreshList(input_name.getText().toString().trim());
            }
        });

        input_name.setText(queryText);*/

        dialog_list.setAdapter(mAdapter);
        // 设置按钮监听
        // 设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点�?
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
//		this.setAnimationStyle(R.style.PopupAnimation);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(window.getResources().getColor(
                R.color.transparent));
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // 添加pop窗口关闭事件
        this.setOnDismissListener(new poponDismissListener());
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在�?�择框外面则�?毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {
            @SuppressLint("ClickableViewAccessibility")
            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int heightmin = mMenuView.findViewById(R.id.pop_layout)
                        .getBottom();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                        if (y > heightmin) {
                        }
                        dismiss();
                    }
                }
                return true;
            }
        });
    }

    public void setListener(OnItemClickListener onItemClickListener) {
        dialog_list.setOnItemClickListener(onItemClickListener);
    }

    /**
     * 弹出的popWin关闭的事件，主要是为了将背景透明度改回来
     *
     * @author cg
     */
    class poponDismissListener implements OnDismissListener {

        @Override
        public void onDismiss() {
            WindowManager.LayoutParams params = window.getWindow().getAttributes();
            params.alpha = 1f;
            window.getWindow().setAttributes(params);
        }

    }


    public class LocationAdapter extends NewBaseAdapter {
        List<Location> locations;
//		private Context mContext;

        public LocationAdapter() {
//			mContext = context;
            locations = YCHSInstance.getInstance().getLocations();
        }


        @Override
        public int getCount() {
            return locations == null ? 0 : locations.size();
        }

        @Override
        public Object getItem(int position) {
            return locations.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(window, R.layout.item_select, null);
            }
            TextView locationName = (TextView) convertView.findViewById(R.id.item_name2);
            Location selectItem = (Location) getItem(position);
            locationName.setText(selectItem.getRESOURCE_NAME());
            return convertView;
        }

        public void refreshList(String text) {
            List<Location> tempLocations = new ArrayList<Location>();
            if (text != null && !text.isEmpty()) {
                for (Location item : locations) {
                    if (item.getRESOURCE_NAME().contains(text)) {
                        tempLocations.add(item);
                    }
                }
                locations = tempLocations;
            }
            notifyDataSetChanged();
        }
    }
}